package com.example.testdemo.recyclerview_gallery_load.gallery;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;

import com.example.testdemo.recyclerview_gallery_load.loadmore.LoadMoreAdapter;


/**
 * Created by zhangwenxiang on 2018/8/21.
 */

public class GalleryRecyclerView2 extends RecyclerView {
    public GalleryRecyclerView2(Context context) {
        super(context);
    }

    public GalleryRecyclerView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GalleryRecyclerView2(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private int oldPos;
    private Handler handler = new Handler(){};
    private LoadMoreAdapter mAdapter;


    public void setLoadMoreAdapter(LoadMoreAdapter mAdapter) {
        this.mAdapter = mAdapter;
        setAdapter(mAdapter);
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
        if (state != RecyclerView.SCROLL_STATE_IDLE) {
            return;
        }
        //加载更多监听
        LinearLayoutManager l = (LinearLayoutManager)this.getLayoutManager();
        final int endCompletelyPosition = l.findLastCompletelyVisibleItemPosition();
        Log.i("SpeedRecyclerView", "onScrollStateChanged last = "+l.findLastCompletelyVisibleItemPosition()+ " count="+ mAdapter.getItemCount() +"canbottom= "+this.canScrollHorizontally(1));
        if (endCompletelyPosition == mAdapter.getItemCount() -1) {
            if(mAdapter.getIsCanLoadMore()){
                mAdapter.loadMore();
            }
        }
    }
}
